package com.yanqu.road.dao.impl.activity.shuihucard;

import com.alibaba.fastjson.JSONArray;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shuihu.ShuiHuUserData;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ShuiHuUserDataDaoImpl extends TempDataObjectDao<ShuiHuUserData> {

    public static String getShuiHuUserDataSql = "select * from t_u_activity_shuihu_user_data where activityId=? and userId=?";

    public ShuiHuUserData getShuiHuUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuiHuUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateShuiHuUserDataSql = "update t_u_activity_shuihu_user_data set score=?,point=?,cardProp=?,combProp=?,gotCardList=?,autoReward=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(ShuiHuUserData shuiHuUserData) {
        shuiHuUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuUserData.getPoint()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(shuiHuUserData.getCardProp())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(shuiHuUserData.getCombProp())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(shuiHuUserData.getGotCardIdList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuiHuUserData.isAutoReward()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuUserData.getUserId()));
        if (getDb().executeNoneQuery(updateShuiHuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuiHuUserDataSql = "insert into t_u_activity_shuihu_user_data(activityId,userId,score,point,cardProp,combProp,gotCardList,autoReward) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ShuiHuUserData shuiHuUserData) {
        shuiHuUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuUserData.getPoint()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(shuiHuUserData.getCardProp())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(shuiHuUserData.getCombProp())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(shuiHuUserData.getGotCardIdList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuiHuUserData.isAutoReward()));
        if (getDb().executeNoneQuery(insertShuiHuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String getAutoRewardUserDataSql = "select * from t_u_activity_shuihu_user_data where activityId=? and autoReward=0";

    public Map<Long, ShuiHuUserData> getAutoRewardUserDataMap(int activityId) {
        Map<Long, ShuiHuUserData> userDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAutoRewardUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuiHuUserData parse = parse(rs);
                userDataMap.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userDataMap;
    }

    @Override
    protected ShuiHuUserData parseData(ResultSet rs) throws SQLException {
        ShuiHuUserData shuiHuUserData = new ShuiHuUserData();
        shuiHuUserData.setActivityId(rs.getInt("activityId"));
        shuiHuUserData.setUserId(rs.getLong("userId"));
        shuiHuUserData.setScore(rs.getInt("score"));
        shuiHuUserData.setPoint(rs.getInt("point"));
        String cardProp = rs.getString("cardProp");
        shuiHuUserData.setCardProp(StringUtils.isNullOrEmpty(cardProp) ? new Property() : PropertyHelper.parseStringToProperty(cardProp));
        String combProp = rs.getString("combProp");
        shuiHuUserData.setCombProp(StringUtils.isNullOrEmpty(combProp) ? new Property() : PropertyHelper.parseStringToProperty(combProp));
        String gotCardList = rs.getString("gotCardList");
        shuiHuUserData.setGotCardIdList(StringUtils.isNullOrEmpty(gotCardList) ? new ArrayList<>() : JSONArray.parseArray(gotCardList, Integer.class));
        shuiHuUserData.setAutoReward(rs.getBoolean("autoReward"));
        return shuiHuUserData;
    }

}
